// xc_lshop/integralmall/signin/index.js
var app = getApp()
var actsignbill = require('../model/integral_sign')
// var actbill = require('../../../xc/model/diy_index')
var actbill = require('../model/integralpro_contents')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],
  
  /**
   * 页面的初始数据
   */
  data: {
    conditon: {
      date: 'week'
    },
    open: false,
    nowdate: "",
    ruleshow:false,
    taskid:"1",
    CustomBar:app.globalData.CustomBar,
    basicurl: app.globalData.basicurl,
    second:-1,
    finish:false
  },
  //浏览完成
  downfinish(e){
    console.log(e)
    this.setData({
      finish:true
    })
    this.finishtask("look")
  },
  finishtask(keyval){
    var that = this;
    actsignbill.finishtask(keyval).then(function (reslutdata) {
      console.log("5555555555555")
      console.log(reslutdata)

      if (reslutdata["status"] == 1) {
        that.setData({
          'taskstatus.look':2
        })
      }
    })
  },
  //去浏览
  lookstart(){
    var that=this;
    if(this.data.second!=-1){
       return false
    }

    var second= this.data.integralconfig.signin.second;
    this.setData({
      second:second  * 1000
    })
    console.log(this.data.second)
    wx.pageScrollTo({
      selector:"#iprolist",
      duration: 300
    })
  },
  onnavChange(e){
    console.log(e)
    this.setData({
      taskid:e.detail.name
    })
  },
  onruleClose(){
     this.setData({
       ruleshow:!this.data.ruleshow
     })
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {};
      }
      strwhere["status"] = 1;
      strwhere["bpid"] = 0;
      strwhere["spid"] =0;
      // strwhere["seachval"] = this.data.seachval;
      that.setData({ modellist: [] });
    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};

      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {

         for(var a in result["obj"]["modellist"] ){
           if(result["obj"]["modellist"][a]){
            result["obj"]["modellist"][a]["snapshot"] = JSON.parse(result["obj"]["modellist"][a]["snapshot"])
           }
         }     
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        }
        else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);
    });
  },
  //领取
  getcores(e){
    // console.log(e.currentTarget.dataset)
    var datset=e.currentTarget.dataset
    var key=datset.key;
    var that = this;
    actsignbill.getcores(key).then(function (reslutdata) {
      console.log("reslutdata")
      if (reslutdata["status"] == 1) {
        console.log(reslutdata)
        var text="成功领取"+ reslutdata['obj']['integral']+that.data.integralname;
        wx.showToast({
          icon:"none",
          title: text,
        })
        var name="taskstatus."+key;
        console.log(reslutdata)
        that.setData({
          [name]:true,
          member:reslutdata['obj']['member']
        })
        app.globalData.member=reslutdata['obj']['member']

      }else{
        wx.showToast({
          icon:"error",
          title: reslutdata["message"],
        })
      }
    })
  },
  // 登录注册
  login(e) {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
  afterlogin: function (options, config, userinfo) {
    var that = this;
    this.setData({
      member: userinfo
    })
    // if (userinfo['member_id']==0) {
    //   this.login()
    // }
  },
  xcloaddata: function (options, config) {
    if(this.data.basicurl==-1){
      this.setData({
        basicurl:app.globalData.basicurl
      })
    }

    var that=this;
    var update = {};
    if(this.data.basicurl==-1){
      update['basicurl']=app.globalData.basicurl
    }
    if (config.integral) {
      update['integralconfig'] = config.integral;
      update['newbietask'] = config.integral.signin.newbietask;
      update['daybietask'] = config.integral.signin.daybietask;

    }
    var nowdate = this.getnowdate();
    update['nowdate'] = nowdate;
    this.setData(update)
    this.getsign()
    this.gettaskstatus()
    this.getpagelist(that,1)
  },
  //获取任务状态
  gettaskstatus(){
    var that = this;
    actsignbill.gettaskstatus().then(function (reslutdata) {
      console.log("reslutdata")
      if (reslutdata["status"] == 1) {
        console.log(reslutdata)
        that.setData({
          taskstatus:reslutdata['obj']
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //上个月
  premonth() {
    var date = this.data.conditon.date
    var datearray = date.split("-")
    var y = datearray['0'];
    var m = datearray['1'];
    if (m - 1 > 0) {
      m = m - 1
    } else {
      m = 12;
      y--;
    }
    var nowdate = y + "-" + m
    this.setData({
      "conditon.date": nowdate
    })
    this.getsign()
  },
  //下个月
  nextmonth() {
    var date = this.data.conditon.date
    if (date == this.data.nowdate) {
      return;
    }
    var datearray = date.split("-")
    var y = datearray['0'];
    var m = datearray['1'];
    console.log
    if (m * 1 + 1 > 12) {
      m = 1;
      y++;
    } else {
      m = m * 1 + 1;
    }
    var nowdate = y + "-" + m
    this.setData({
      "conditon.date": nowdate
    })
    this.getsign()
  },
  //签到打开关闭
  openorclose() {
    var open = this.data.open;
    var update = {};
    if (open) {
      update['conditon.date'] = 'week';
    } else {
      update['conditon.date'] = this.getnowdate();
    }
    update['signdata'] = [];
    console.log(update)
    update['open'] = !open;
    this.setData(update)
    this.getsign()
  },
  getnowdate() {
    var myDate = new Date();
    var y = myDate.getFullYear();
    var m = myDate.getMonth() + 1;
    return y + '-' + m
  },
  getsign() {
    var that = this;
    actsignbill.getrecord(this.data.conditon).then(function (reslutdata) {
      console.log("reslutdata")
      if (reslutdata["status"] == 1) {
        console.log(reslutdata)
        that.setData({
          nowtime: reslutdata['obj']['time'],
          signdata: reslutdata['obj']['data'],
          signday: reslutdata['obj']['day'],
          todaysigned: reslutdata['obj']['todaysign']
        })
      }
    })
  },
  //签到
  signin() {
    var that = this;
    actsignbill.signin().then(function (reslutdata) {
      console.log("33333")
      if (reslutdata["status"] == 1) {
        that.getsign()
        console.log(reslutdata)
        app.globalData.member=reslutdata['obj']['member']
        that.setData({
          member:reslutdata['obj']['member']
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
})